import { Injectable } from '@angular/core';
import { BaseRequestOptions,Http,Response, HTTP_PROVIDERS,Headers,URLSearchParams} from '@angular/http';
import { GlobalService } from './global.service';
import {BaseService} from './base.service';
import { Proj } from '../model/proj.model';

@Injectable()
export class ProjService extends BaseService
{
  headers:Headers;
  constructor(private http:Http,private globalService:GlobalService) {
    super();
  }

  addProj(proj:Proj)
  {
    if(this.globalService.getUser())
    {
      this.headers.set("x-token",this.globalService.getUser().token);
      var params = JSON.stringify(proj);
      return this.http.post(BaseService.apiUrl+'/projects/',params,{headers:this.headers});
    }
  }

  updateProj(proj:Proj)
  {
    if(this.globalService.getUser())
    {
      this.headers.set("x-token",this.globalService.getUser().token);
      var params = JSON.stringify(proj);
      return this.http.put(BaseService.apiUrl+'/projects/'+proj._id,params,{headers:this.headers});
    }
  }
  getProj(projId:string)
  {
    if(this.globalService.getUser())
    {
      this.headers.set("x-token",this.globalService.getUser().token);
      return this.http.get(BaseService.apiUrl+'/projects/'+projId,{headers:this.headers});
    }
  }

  getMyProjs()
  {
    if(this.globalService.getUser())
    {
      this.headers.set("x-token",this.globalService.getUser().token);
      return this.http.get(BaseService.apiUrl+'/projects/user',{headers:this.headers});
    }
  }
  deleteProj(projId:string)
  {
    this.headers.set("x-token",BaseService.user.token);
    return this.http.delete(BaseService.apiUrl+'/projects/'+projId,{headers:this.headers});
  }
}
